let rec power x n = match n with 
                    n <= 0 -> invalid_arg "puissance negative"
                   |0      -> 1
				   |n      -> x * power x (n-1);;
				
let rec application polynome x = match polynome with
                             [] -> 0
						    |(a,b)::polynome -> a * (power x b) + application polynome x;;
								
let rec add polynome1 polynome2 = match (polynome1,polynome2) with
   ([],[]) -> []
  |([],p2) -> p2
  |(p1,[]) -> p1
  |((a1,b1)::p1,(a2,b2)::p2) -> if b1=b2 then
                                    if a1+a2<>0 then add polynome1 polynome2 
                                    else ((a1+a2),b2)::add polynome1 polynome2
                                else
                                    if b1 < b2 then (a1,b1)::add p1 ((a2,b2)::p2)
                                    else (a2,b2)::add ((a1,b1)::p1) p2;;

let rec soustract polynome1 polynome2 = match (polynome1,polynome2) with
                                ([],[]) -> []  
                               |([],(a1,b1)::p2) -> (-a1,b1)::soustract ([],p2)
						       |(p1,[]) -> p1
			                   |((a1,b1)::p1,(a2,b2)::p2) -> if b1=b2 then
                                                                 if a1-a2<>0 then soustract polynome1 polynome2
                                                                            else ((a1-a2),b2)::soustract polynome1 polynome2
                                                                      else
                                                              if b1 < b2 then (a1,b1)::soustract p1 ((a2,b2)::p2)
                                                                         else (0-a2,b2)::soustract ((a1,b1)::p1) p2;;
																		   
let rec deriv polynome = match polynome with 
                         [] -> []
						|(a1,b1)::polynome -> (a1*b1,b1-1) :: deriv polynome;;

power 5 2;;
application [(2,2);(2,3);(4,4)] 2;;
add [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;

soustract [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;
deriv [(2,2);(2,3);(4,4)];;
